<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;
use think\Db;
use app\common\model\UserAccount;
/**
 * game逻辑
 */
class Game extends LogicBase
{
    

    
    /**
     * 获取game列表
     */
    public function getgameList($where = [], $field = 'a.*,c.name as category_name', $order = '')
    {
        
        $this->modelgame->alias('a');
        

        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        
        return $this->modelgame->getList($where, $field, $order);
    }
    
    /**
     * 获取game列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        !empty($data['category_id']) && $where['a.category_id'] = $data['category_id'];
        !empty($data['search_data']) && $where['a.name'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * game信息编辑
     */
    public function gameEdit($data = [])
    {
        
        $validate_result = $this->validategame->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validategame->getError()];
        }
        
        $url = url('gameList');


//        $data['content'] = htmlspecialchars_decode($data['content']);
        $result = $this->modelgame->setInfo($data);

        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, 'game' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, 'game操作成功', $url] : [RESULT_ERROR, $this->modelgame->getError()];
    }

    /**
     * 获取game信息
     */
    public function getgameInfo($where = [], $field = 'a.*')
    {
        
        $this->modelgame->alias('a');
        

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelgame->getInfo($where, $field);
    }
    
 //导入excel
    public function excel(){

       set_time_limit(0);
        $excel = request()->file('excel')->getInfo();//excel为file中的name
        vendor("PHPExcel.PHPExcel.IOFactory");
        $objPHPExcel = \PHPExcel_IOFactory::load($excel['tmp_name']);//读取上传的文件
        $arrExcel = $objPHPExcel->getSheet(0)->toArray();//获取其中的数据

        $word = $arrExcel[0];
        array_shift($arrExcel);

        $num = count($arrExcel) -1;
       // dd($arrExcel);
        Db::startTrans();

        foreach ($arrExcel as $key => $value) {
            $data=[
                'username'=>$value[1],
                'nickname'=>$value[1],
                'password'=>$value[1],
            ];
            if($member=$this->modelMember->getInfo(['username'=>$value[1]])){
//                if($value[4]>0){
//                    UserAccount::where(['user_id'=>$member['id'],'wallet_id'=>3])->setInc('balance',$value[4]);
//                    Db::commit();
//                }
                continue;

            }
            try{
                if($value[3] && $member=$this->modelMember->getInfo(['username'=>$value[3]])){
                  $data['leader_id']=$member['id'];
                }
                //注册
                $id=\app\common\model\Member::create($data);
                //创建钱包
                UserAccount::addWallet($id->id);
                if($value[4]>0){
                    UserAccount::where(['user_id'=>$id->id,'wallet_id'=>3])->setInc('balance',$value[4]);
                }
                Db::commit();
            }catch (\Exception $e){
                echo $e->getMessage();
                Db::rollback();

            }

        }



           echo '导入成功'; die;

    }

    /**

    
    /**
     * game删除
     */
    public function gameDel($where = [])
    {
        
        $result = $this->modelgame->deleteInfo($where);
        
        $result && action_log('删除', 'game删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, 'game删除成功'] : [RESULT_ERROR, $this->modelgame->getError()];
    }

    /**
     * 获取分类信息
     */
    public function getgameCategoryInfo($where = [], $field = true)
    {

        return $this->modelGameCategory->getInfo($where, $field);
    }

    /**
     * 获取游戏分类列表
     */
    public function getgameCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {

        return $this->modelGameCategory->getList($where, $field, $order, $paginate);
    }

    /**
     * 游戏分类删除
     */
    public function gameCategoryDel($where = [])
    {

        $result = $this->modelGameCategory->deleteInfo($where);

        $result && action_log('删除', '游戏分类删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '游戏分类删除成功'] : [RESULT_ERROR, $this->modelGameCategory->getError()];
    }

    /**
     * 游戏分类编辑
     */
    public function gameCategoryEdit($data = [])
    {

        $validate_result = $this->validateGameCategory->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateGameCategory->getError()];
        }

        $url = url('gameCategoryList');

        $result = $this->modelGameCategory->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '游戏分类' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelGameCategory->getError()];
    }


    /**
     * 文章删除
     */
    public function update_info($where = [])
    {

        //dd($where);

        $result = $this->modelgame->setInfo($where);


        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }
    
}
